<?php

App::uses('AppController', 'Controller');

class ProductController extends AppController {

	public $uses = array('Product', 'Category');

	public function index() {
		
		$data = $this->Product->getAllProduct();
		$this->set('list', $data);
		
		$category = $this->Category->find('all');
		$this->set('category', $category);
	}
	
	public function edit() {
	
		if ($this->request->is('post')) {
				
			$this->Product->save($this->request->data);
			$this->Session->setFlash('Update product success !', 'flash_custom');
				
			return $this->redirect('/product');
		} else {
				
			$category = $this->Category->find('all', array('conditions' => array('delete_flg' => '0')));
			$this->set('category', $category);
			
			$id = $_GET['id'];
			$data = $this->Product->find('first', array('conditions' => array('id' => $id)));
			$this->set('data', $data);
		}
	
	}
	
	public function delete() {
	
		$id = $_GET['id'];
	
		$data = array('id' => $id, 'delete_flg' => '1');
	
		// This will update Recipe with id 10
		$this->Product->save($data);
	
		return $this->redirect('/product');
	}
	
	public function add() {
	
		$category = $this->Category->find('all', array('conditions' => array('delete_flg' => '0')));
		$this->set('category', $category);
		
		if ($this->request->is('post')) {
	
			$time = explode(' ', microtime(false));
						
			if($this->request->data['Product']['sub_img1']['name'] != '') {

				move_uploaded_file($this->request->data['Product']['sub_img1']['tmp_name'], WWW_ROOT.'img/product_images'.DS.$time[1].$this->request->data['Product']['sub_img1']['name']);
				$this->request->data['Product']['sub_img1'] = 'product_images/'.$time[1].$this->request->data['Product']['sub_img1']['name'];
			} else {
				$this->request->data['Product']['sub_img1'] = $this->request->data['Product']['img1'];
			}
			
			
			if($this->request->data['Product']['sub_img2']['name'] != '') {
			
				move_uploaded_file($this->request->data['Product']['sub_img2']['tmp_name'], WWW_ROOT.'img/product_images'.DS.$time[1].$this->request->data['Product']['sub_img2']['name']);
				$this->request->data['Product']['sub_img2'] = 'product_images/'.$time[1].$this->request->data['Product']['sub_img2']['name'];
			} else {
				$this->request->data['Product']['sub_img2'] = $this->request->data['Product']['img2'];
			}
			
			if($this->request->data['Product']['sub_img3']['name'] != '') {
			
				move_uploaded_file($this->request->data['Product']['sub_img3']['tmp_name'], WWW_ROOT.'img/product_images'.DS.$time[1].$this->request->data['Product']['sub_img3']['name']);
				$this->request->data['Product']['sub_img3'] = 'product_images/'.$time[1].$this->request->data['Product']['sub_img3']['name'];
			} else {
				$this->request->data['Product']['sub_img3'] = $this->request->data['Product']['img3'];
			}						
			
			// If the form data can be validated and saved...
			if ($this->Product->save($this->request->data)) {				
				
				// Set a session flash message and redirect.
				$this->Session->setFlash('Update product success !', 'flash_custom');
	
				return $this->redirect('/product');
			} else {
				
				// Set a session flash message and redirect.
				$this->Session->setFlash('Error ! Please try again.', 'flash_custom_error');
				
				return $this->redirect('/product');
			}
		}
	}
}
